package com.atguigu.day03;


import com.sunfeng.TimeUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * @Author : 孙峰
 * @Description:
 * @Date : 2022/7/2  14:13
 */
@Slf4j(topic = "c.Test01")
public class Test01 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<Integer> futureTask1 = new FutureTask<>(() -> getPriceByS(1));
        FutureTask<Integer> futureTask2 = new FutureTask<>(() -> getPriceByS(2));
        FutureTask<Integer> futureTask3 = new FutureTask<>(() -> getPriceByS(3));

        Thread thread1 = new Thread(futureTask1);
        thread1.start();
        Thread thread2 = new Thread(futureTask2);
        thread2.start();
        Thread thread3 = new Thread(futureTask3);
        thread3.start();
        ExecutorService saveThreadPool = Executors.newFixedThreadPool(3);

        saveThreadPool.execute(() -> {
            try {
                log.info("获取到的结果为{}", futureTask1.get());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        saveThreadPool.execute(() -> {
            try {
                log.info("获取到的结果为{}", futureTask2.get());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        saveThreadPool.execute(() -> {
            try {
                log.info("获取到的结果为{}", futureTask3.get());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        });
        saveThreadPool.shutdown();
    }

    private static Integer getPriceByS(Integer integer) {
        TimeUtils.sleep(integer);
        return integer;
    }
}
